<?php
// this php page is the login page of the application
session_start();
require_once('./inc/database.php');
require_once('./inc/functions.php');
require_once('./class/class.user.php');

//check user login, connect to DB and create new user
if (isset($_POST['login_username']) && isset($_POST['login_password'])) {//user and password was inserted
    db_connect();
    $result = CheckLogin($_POST['login_username'], $_POST['login_password']);

    if ($result == false) {//worng login
        ?>

        <style>#wrongAccess{display:block !important;}</style>

        <?php
    } else {//login correct
        $_SESSION['user'] = $_POST['login_username'];
        $user = new user();
        $user->getby_name($_POST['login_username']);
        
        if ($user->role_type == 1 || $user->role_type == 6) {
            $_SESSION['Admin'] = $_POST['login_username'];
        }
        switch ($result) {
            case 1:
                echo "<script >
								window.location ='main.php';</script>";
                break;
            case 2:
                echo "<script >
								window.location ='main.php';</script>";
                break;
            case 3:
                echo "<script >
						    	window.location ='main.php';</script>";
                break;
            case 5:
                echo "<script >
								  window.location ='main.php';</script>";
                break;
            case 4:
                echo "<script >
								  window.location ='main.php';</script>";
                break;
            case 6:
                echo "<script >
								window.location ='main.php';</script>";
                break;
        }
    }
} else {
    if (isset($_SESSION['user'])) { //remove old user session
        session_destroy();
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <title>RestartIT CRM</title>
    <head>
        <!--meta tags--->

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <meta http-equiv="X-UA-Compatible" content="IE=8" />

        <meta name="author" content="RestartIT LTD - web developing | Shiran & Miri | ">
            <meta name="description" content="" />
            <meta name="keywords" content="" />

            <!-- css implanting--->
            <link href="style/login.css" rel="stylesheet" type="text/css" />
            <link rel="icon"  type="image/png" href="style/images/icon_small.png"  />

            <!-- js implanting-->
            <script src="inc/js/jquery-1.7.min.js"  type="text/javascript"></script>
            <script src="inc/js/core.js" type="text/javascript"></script>
            <script src="./inc/js/jquery.validate.js" type="text/javascript"></script>

            <title>Untitled Document</title>
    </head>
    <style>
        #must_fields
        {
            display:none;
            color:red;	
        }
        body
        {
            background:#FFF;	
        }
    </style>
    <script>
        $(document).ready(function(e) {
            $('#formlogin').validate({
                rules: {
                    login_username: {required: true},
                    login_password: {required: true}

                },
                messages: {
                    login_username: "נא הזן שם משתמש",
                    login_password: "נא הזן סיסמא"
                },
                errorLabelContainer: '#must_fields',
                errorElement: "li",
            });
        });

    </script>

    <body>
        <div id="wrapper"><!-- wraps the site and puts it in the middle-->
            <div id="login">

                <img src="./style/images/login_logo.png" alt="restartIT - IT services and web developing" />


                <div id="login_main">
                    <form id="formlogin" method="post"  action="">
                        <div id="username_place_holder">
                            <span>username : </span>
                            <input tabindex="1" type="text" class="login_input" id="login_username" name="login_username"/>
                        </div>

                        <div id="password_place_holder">
                            <span>password : </span>
                            <input tabindex="2" type="password" class="login_input" id="login_password" name="login_password" />
                            <a onClick="forgot_pass()" >forgot your password?</a>
                        </div>

                        <div id="btn_place_holder">
                            <input tabindex="3" type="submit" id="login_btn" name="login_btn" value="login" />
                        </div>
                    </form>
                    <h4 id="wrongAccess">שם המשמתמש או הסיסמא אינם נכונים</h4>
                </div>

            </div>


        </div><!--wrapper-->
    </body>
</html>
<script>
    function forgot_pass()
    {
        
            alert('נא פנה למנהל המערכת ');
        
    }
</script>


